package com.ruoyi.pcwl.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.pcwl.domain.MediaFiles;
import com.ruoyi.pcwl.domain.MinioFile;
import com.ruoyi.pcwl.domain.enums.MediaTypeEnum;
import com.ruoyi.pcwl.minio.MinioUtil;
import com.ruoyi.pcwl.service.IMediaFilesService;
import io.minio.messages.Item;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;


import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 媒资信息Controller
 *
 * @author ruoyi
 * @date 2024-01-17
 */
@RestController
@RequestMapping("/mediaFiles")
public class MediaFilesController extends BaseController
{
    @Autowired
    private IMediaFilesService mediaFilesService;


//    上传轮播图
//    @PostMapping("/uploadCarousel")
//    public AjaxResult upload(@RequestParam(name = "file", required = false) MultipartFile[] file) {
//        if (file == null || file.length == 0) {
//            return AjaxResult.error("上传文件不能为空");
//        }
//        List<String> orgfileNameList = new ArrayList<>(file.length);
//        for (MultipartFile multipartFile : file) {
//            String orgfileName = multipartFile.getOriginalFilename();
//            orgfileNameList.add(orgfileName);
//            try {
//                //文件上传
//                InputStream in = multipartFile.getInputStream();
//                minioClient.putObject(
//
//                        PutObjectArgs.builder().bucket(bucketName).object(orgfileName).stream(
//                                        in, multipartFile.getSize(), -1)
//                                .contentType(multipartFile.getContentType())
//                                .build());
//                in.close();
//            } catch (Exception e) {
//                log.error(e.getMessage());
//                return AjaxResult.error("上传失败");
//            }
//        }
//        Map<String, Object> data = new HashMap<String, Object>();
//        data.put("bucketName", bucketName);
//        data.put("fileName", orgfileNameList);
//        return AjaxResult.success("上传成功", data);
//    }


    @GetMapping("/listMedia")
    public AjaxResult listMedia() throws Exception {
        //获取bucket列表

        ArrayList<MinioFile> mediaFileList = MinioUtil.getMediaFileList(MediaTypeEnum.CAROUSEL);
        for (MinioFile item : mediaFileList) {
            System.out.println(item.getSize() + ", " + item.getObjectName());
        }
        return AjaxResult.success();
    }



    /**
     * 查询媒资信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:files:list')")
    @GetMapping("/list")
    public TableDataInfo list(MediaFiles mediaFiles)
    {
        startPage();
        List<MediaFiles> list = mediaFilesService.selectMediaFilesList(mediaFiles);
        return getDataTable(list);
    }

    /**
     * 导出媒资信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:files:export')")
    @Log(title = "媒资信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MediaFiles mediaFiles)
    {
        List<MediaFiles> list = mediaFilesService.selectMediaFilesList(mediaFiles);
        ExcelUtil<MediaFiles> util = new ExcelUtil<MediaFiles>(MediaFiles.class);
        util.exportExcel(response, list, "媒资信息数据");
    }

    /**
     * 获取媒资信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:files:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(mediaFilesService.selectMediaFilesById(id));
    }

    /**
     * 新增媒资信息
     */
    @PreAuthorize("@ss.hasPermi('system:files:add')")
    @Log(title = "媒资信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MediaFiles mediaFiles)
    {
        return toAjax(mediaFilesService.insertMediaFiles(mediaFiles));
    }

    /**
     * 修改媒资信息
     */
    @PreAuthorize("@ss.hasPermi('system:files:edit')")
    @Log(title = "媒资信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MediaFiles mediaFiles)
    {
        return toAjax(mediaFilesService.updateMediaFiles(mediaFiles));
    }

    /**
     * 删除媒资信息
     */
    @PreAuthorize("@ss.hasPermi('system:files:remove')")
    @Log(title = "媒资信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mediaFilesService.deleteMediaFilesByIds(ids));
    }
}
